{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bayesian Belief Network is created using pgmpy and some simple queries are done on the network.\n",
    "This network is an Alarm Byaesian Beleief Network which is mentioned in Bayesian Artificial Intelligence - Section 2.5.1 (https://bayesian-intelligence.com/publications/bai/book/BAI_Chapter2.pdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing Library\n",
    "from pgmpy.models import BayesianModel\n",
    "from pgmpy.inference import VariableElimination"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Defining network structure\n",
    "\n",
    "alarm_model = BayesianModel([('Burglary', 'Alarm'), \n",
    "                              ('Earthquake', 'Alarm'),\n",
    "                              ('Alarm', 'JohnCalls'),\n",
    "                              ('Alarm', 'MaryCalls')])\n",
    "\n",
    "#Defining the parameters using CPT\n",
    "from pgmpy.factors.discrete import TabularCPD\n",
    "\n",
    "cpd_burglary = TabularCPD(variable='Burglary', variable_card=2,\n",
    "                      values=[[.999], [0.001]])\n",
    "cpd_earthquake = TabularCPD(variable='Earthquake', variable_card=2,\n",
    "                       values=[[0.998], [0.002]])\n",
    "cpd_alarm = TabularCPD(variable='Alarm', variable_card=2,\n",
    "                        values=[[0.999, 0.71, 0.06, 0.05],\n",
    "                                [0.001, 0.29, 0.94, 0.95]],\n",
    "                        evidence=['Burglary', 'Earthquake'],\n",
    "                        evidence_card=[2, 2])\n",
    "cpd_johncalls = TabularCPD(variable='JohnCalls', variable_card=2,\n",
    "                      values=[[0.95, 0.1], [0.05, 0.9]],\n",
    "                      evidence=['Alarm'], evidence_card=[2])\n",
    "cpd_marycalls = TabularCPD(variable='MaryCalls', variable_card=2,\n",
    "                      values=[[0.1, 0.7], [0.9, 0.3]],\n",
    "                      evidence=['Alarm'], evidence_card=[2])\n",
    "\n",
    "# Associating the parameters with the model structure\n",
    "alarm_model.add_cpds(cpd_burglary, cpd_earthquake, cpd_alarm, cpd_johncalls, cpd_marycalls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Checking if the cpds are valid for the model\n",
    "alarm_model.check_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NodeView(('Burglary', 'Alarm', 'Earthquake', 'JohnCalls', 'MaryCalls'))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Viewing nodes of the model\n",
    "alarm_model.nodes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OutEdgeView([('Burglary', 'Alarm'), ('Alarm', 'JohnCalls'), ('Alarm', 'MaryCalls'), ('Earthquake', 'Alarm')])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Viewing edges of the model\n",
    "alarm_model.edges()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Burglary _|_ Earthquake)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Checking independcies of a node\n",
    "alarm_model.local_independencies('Burglary')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Burglary _|_ Earthquake)\n",
       "(Burglary _|_ MaryCalls, JohnCalls | Alarm)\n",
       "(Burglary _|_ JohnCalls | MaryCalls, Alarm)\n",
       "(Burglary _|_ MaryCalls, JohnCalls | Alarm, Earthquake)\n",
       "(Burglary _|_ MaryCalls | JohnCalls, Alarm)\n",
       "(Burglary _|_ JohnCalls | MaryCalls, Alarm, Earthquake)\n",
       "(Burglary _|_ MaryCalls | Alarm, JohnCalls, Earthquake)\n",
       "(Earthquake _|_ Burglary)\n",
       "(Earthquake _|_ MaryCalls, JohnCalls | Alarm)\n",
       "(Earthquake _|_ MaryCalls, JohnCalls | Burglary, Alarm)\n",
       "(Earthquake _|_ JohnCalls | MaryCalls, Alarm)\n",
       "(Earthquake _|_ MaryCalls | JohnCalls, Alarm)\n",
       "(Earthquake _|_ JohnCalls | Burglary, MaryCalls, Alarm)\n",
       "(Earthquake _|_ MaryCalls | Burglary, JohnCalls, Alarm)\n",
       "(JohnCalls _|_ Burglary, MaryCalls, Earthquake | Alarm)\n",
       "(JohnCalls _|_ MaryCalls, Earthquake | Burglary, Alarm)\n",
       "(JohnCalls _|_ Burglary, Earthquake | MaryCalls, Alarm)\n",
       "(JohnCalls _|_ Burglary, MaryCalls | Alarm, Earthquake)\n",
       "(JohnCalls _|_ Earthquake | Burglary, MaryCalls, Alarm)\n",
       "(JohnCalls _|_ MaryCalls | Burglary, Alarm, Earthquake)\n",
       "(JohnCalls _|_ Burglary | MaryCalls, Alarm, Earthquake)\n",
       "(MaryCalls _|_ Burglary, JohnCalls, Earthquake | Alarm)\n",
       "(MaryCalls _|_ JohnCalls, Earthquake | Burglary, Alarm)\n",
       "(MaryCalls _|_ Burglary, JohnCalls | Alarm, Earthquake)\n",
       "(MaryCalls _|_ Burglary, Earthquake | JohnCalls, Alarm)\n",
       "(MaryCalls _|_ JohnCalls | Burglary, Alarm, Earthquake)\n",
       "(MaryCalls _|_ Earthquake | Burglary, JohnCalls, Alarm)\n",
       "(MaryCalls _|_ Burglary | Alarm, JohnCalls, Earthquake)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Listing all Independencies\n",
    "alarm_model.get_independencies()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
